<template>
  <!-- 净借记限额可用额度预警设置录入设置页面弹出框-->
  <el-dialog
    title="净借记限额可用额度预警设置录入设置"
    center
    :visible.sync="open"
    width="1024px"
    :close-on-click-modal="false"
    @close="closeFn"
  >
    <el-form ref="settingsForm" :model="formData" :rules="rules" label-width="135px">
      <el-card class="form-card txnInfo-card">
        <div slot="header" class="label-header-msg">
          <span>基本信息</span>
        </div>
        <el-row>
          <el-col :span="12">
            <el-form-item label="被设置清算行行号：" prop="setStBrno">
              <el-input v-model="formData.setStBrno" placeholder="被设置清算行行号" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="被设置清算行行名：" prop="setStBrnoName">
              <el-input v-model="formData.setStBrnoName" placeholder="被设置清算行行名" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="预警开启关闭类型：" prop="switchType">
              <el-select v-model="formData.switchType" placeholder="请选择" style="width: 100%" disabled>
                <el-option
                  v-for="dict in dictMap.SWITCHTYPECODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="货币币种：" prop="curcd">
              <el-select v-model="formData.curcd" placeholder="请选择" style="width: 100%" disabled>
                <el-option
                  v-for="dict in dictMap.CURCD"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="当前可用额度预警值：" prop="warnValue">
              <el-input v-model="formData.warnValue" placeholder="当前可用额度预警值" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="最后设置成功时间：" prop="currencyTime">
              <el-date-picker
                disabled
                v-model="formData.currencyTime"
                type="date"
                value-format="yyyyMMdd"
                placeholder="选择时间"
                style="width: 100%"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
      <el-card class="form-card txnInfo-card">
        <div slot="header" class="label-header-msg">
          <span>参数设置</span>
        </div>
        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="货币币种：" prop="curcd2">
              <el-input v-model="formData.curcd2" placeholder="货币币种" disabled />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row type="flex">
          <el-col :span="12">
            <el-form-item label="预警开启关闭类型：" prop="switchType2">
              <el-select v-model="formData.switchType2" placeholder="请选择" style="width: 100%" @change="switchChange">
                <el-option
                  v-for="dict in dictMap.SWITCHTYPECODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="可用额度预警值：" prop="warnValue2">
              <HtAmountInput v-model="formData.warnValue2" placeholder="可用额度预警值" :disable="isControl" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="备注：" prop="remarks">
              <el-input type="textarea" v-model="formData.remarks" placeholder="备注" maxlength="256" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="isLoading">提交</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { save } from "@/api/cnaps/nets/AcctLimtWarn/net_debit_entry.js";
import HtAmountInput from "@/views/components/HtAmountInput/index.vue";
export default {
  name: "warningSetting",
  components: {HtAmountInput},
  props: {
    dictMap: {
      type: Object,
      default: () => {
        return {};
      },
    },
    formData: {
      type: Object,
      default: () => {
        return {};
      },
    },
  },
  data() {
    return {
      isControl: false,
      // 是否显示弹出层
      open: false,
      isLoading: false,
      rules: {
        switchType2: [
          {
            required: true,
            message: "请选择类型",
            trigger: "change",
          },
        ],
        warnValue2: [
          {
            required: true,
            message: "必填",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {},
  methods: {
    // 表单重置
    reset() {
      this.resetForm("settingsForm");
      // this.$set(this, "form", {});
    },
    closeFn() {
      this.open = false;
    },
    show() {
      this.isControl = false;
      this.open = true;
    },
    // 预警开启关闭类型切换方法
    switchChange(val) {
      console.log(val, "预警开启关闭类型");
      if (val === "ST00") {
        this.formData.warnValue2 = "";
        this.$set(this.rules.warnValue2[0], "required", true);
        this.$set(this, "isControl", false);
      } else {
        this.formData.warnValue2 = "0";
        this.$set(this.rules.warnValue2[0], "required", false);
        this.$set(this, "isControl", true);
      }
    },
    submitForm() {
      this.$refs["settingsForm"].validate((valid) => {
        if (valid) {
          if (this.formData.switchType2 === "ST00" && this.formData.warnValue2 <= 0) {
            this.msgWarning("预警开启关闭类型设置为开启时，可用额度预警值应该大于零");
            return;
          }
          this.isLoading = true;
          save(this.formData)
            .then((res) => {
              this.msgSuccess("提交成功！");
              this.isLoading = false;
              this.open = false;
              this.$emit("editComplete", true);
            })
            .catch((err) => {
              this.isLoading = false;
            });
        }
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
